package com.ycu.epidemic.controller;

import com.ycu.epidemic.bean.UserInfo;
import com.ycu.epidemic.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
    *
     * @Param user  封装前台请求数据
    * @Author:lijinhua
    * @Date:2020/7/12
    */
    @RequestMapping("/login")
    public String login(UserInfo user, Model model, HttpSession session) {
        UserInfo u = userService.findByAccount(user);
        if (u == null) {
            model.addAttribute("msg", "账号或密码不正确");
            return "login";
        }
        if (u.getPassword().equals(user.getPassword())) {
            //登录成功
            session.setAttribute("loginUser", u);
            return "redirect:/main.jsp";
        }
        session.setAttribute("msg", "账号或密码不正确");
        return "login";
    }
    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/epidemic.jsp";
    }


}